package com.liuxing.javaSpringBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import com.liuxing.javaSpringBoot.modules.test.entity.City;
import com.liuxing.javaSpringBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File CityController.java
 * @Auth liuxing
 * @Date 2021/8/19 10:54
 * @Email liuxing997@foxmail.com
 */
@RestController
@RequestMapping("/api")
public class CityController {

    @Autowired
    private CityService cityService;

     /**
     * 添加城市信息
     * 127.0.0.1/api/city --- post
     * {"cityName":"dream", "localCityName":"梦幻之城", "countryId":"522"}
     */
    @PostMapping(value = "/city", consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }

     /**
     * 根据城市ID更新城市信息
     * 127.0.0.1/api/city --- put
     * cityName=shanghai, localCityName=上海,cityId=1890
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city) {
        return cityService.updateCity(city);
    }

     /**
     * 根据城市ID删除城市信息
     * 127.0.0.1/api/city/2255 --- delete
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId) {
        return cityService.deleteCityByCityId(cityId);
    }

     /**
     * 根据城市ID查询城市信息
     * 127.0.0.1/api/city/1890 --- get
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId){
        return cityService.getCityByCityId(cityId);
    }

     /**
     * 根据国家ID查询所有城市信息
     * 127.0.0.1/api/cites/522 --- get
     */
    @GetMapping("/cites/{countryId}")
    public List<City> getCitesByCountryId(@PathVariable int countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

     /**
     * 分页查询
     * 127.0.0.1/api/cities--- post
     */
    @PostMapping(value = "/cities",consumes = "application/json")
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean){
        return cityService.getCitiesBySearchBean(searchBean);
    }

     /**
     * 批量操作之插入
     * 127.0.0.1/api/batch/cities--- post
     * [{"cityName":"dream1", "localCityName":"梦幻之城1", "countryId":"522"},
     * {"cityName":"dream2", "localCityName":"梦幻之城2", "countryId":"522"}]
     */
    @PostMapping(value = "/batch/cities",consumes = "application/json")
    public Result<Object> insertCities(@RequestBody List<City> cities) {
        return cityService.insertCities(cities);
    }
}
